using System;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;

public class CubeManager : MonoBehaviour
{
	/// <summary>
	/// A class to handle the spawning of cubes and to set values affecting the cube
	/// through one class.
	/// </summary>
	
	// List of cubes, allows to keep track of all cubes on the screen.
	private List<Cube> listOfCubes = new List<Cube>();
	
	public UnityEngine.GameObject newCubePrefab;
	
	// Keeps a tally of the cubes on the screen, as well as allowing a maximum
	// number of cubes to spawn, to manage the frame rate better.
	private int _maxNumberOfCubes = 5;
	
	// A simple check to check whether 4 cubes have been connected, meaning the
	// player has scored.
	//private bool fourConnected = false;
		
	#region Getters
	
//	public int MaxNumberOfCubes
//	{
//		get { return _maxNumberOfCubes; }
//	}
	
	#endregion
	
	public bool CanCreateCube()
	{
		if (listOfCubes.Count < _maxNumberOfCubes)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public void Start()
	{
//		UnityEngine.Random r1 = new UnityEngine.Random();
//		Random random = new Random();
//		int randomNumber = random.Next(0,4);
	}
	
	void OnGUI()
	{
		// this renders the ui button at the top left every frame, and if clicked returns tru so it does what inside the if staement
		if (GUI.Button (new Rect(20,20,40,20), "Spawn"))
		{
			//check if theres < 5 cubes already spawned for now
			if (CanCreateCube())
			{
				//spawns new cube
				GameObject newCube = Instantiate(newCubePrefab) as GameObject;

				// gets the BAseCube script specifically from the spawned object
				Cube selfBaseCube = newCube.gameObject.GetComponent<Cube>();

				//newCube.renderer.material.color = new Color(0.5f,0.5f,0.5f);

				//adds the cube to a list
				listOfCubes.Add (selfBaseCube);

				//attached cube to the grid, so its a child and shares coordinates locally to the grid
				selfBaseCube.transform.parent = GameObject.Find("Grid").transform;

				//unfinished rickys code to give the cube a location which is should change direction (when it hits the edge of the grid)
				BaseGrid.SelfBaseGrid.GetNextChangeDirPos(selfBaseCube.transform.localPosition, selfBaseCube.CubeVelocity);
			}
		}
	}
	
	void Update()
	{
		
	}
	
	public List<Cube> GetAllCubes
	{
		get { return listOfCubes; }
	}
}

